package Controllers;

import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import sun.awt.GlobalCursorManager;

import Boundry.FindRequestGUI;
import Boundry.SubmitRequestPanel;
import Boundry.GenericTools.RequestTableListPanel;
import Controllers.GenericToolsForController.BaseClass;
import Controllers.GenericToolsForController.PDFfile;
import Controllers.GenericToolsForController.WorkerRolls;
import Entity.InfoSystem;
import Entity.Request;

public class UserController extends BaseClass{

	private FindRequestGUI findreqGUI; // finding request
	private SubmitRequestPanel submitRequestPanel=null;
	private List<InfoSystem> InfoStstems;
	
	private static  UserController Instance = null;
	private UserController(){}
	public static UserController getInstance()
	{
		if(Instance == null)
		{
			Instance = new UserController();
		}
		return Instance;
	}
	
	
	public void searchRequest()
	{
		//TODO to be implemented 
		//Request req = RequestManager.getInstance().getRequestByID(ReqID);
				findreqGUI = new FindRequestGUI();
				this.setListenerToInputPanel(findreqGUI);
				GlobalData.guiMangaer.switchScreen(findreqGUI);
				findreqGUI.getBtnSearch().addActionListener(this);
		
		
		
		
	}
	
	
	public void SubmitNewRequest()
	{
		submitRequestPanel = new SubmitRequestPanel();
		setListenerToInputPanel(submitRequestPanel);	
		
		SwingWorker worker = new SwingWorker<Void, Void>() {
		@Override
		 public Void doInBackground() 
		 {
			System.out.println(" RequestManager.getInstance().getAllRequestToCloseRequest() ");
			InfoStstems = InfoSystemsManager.getInstance().getAllInfoStstems();
			if(InfoStstems == null)
			{
				JOptionPane.showMessageDialog(new JFrame(),"There Is Not Info System in the Progream", "",JOptionPane.DEFAULT_OPTION);
				GlobalData.guiMangaer.switchScreen(GlobalData.mainScreen);
			}
			else
			{
				GlobalData.guiMangaer.switchScreen(submitRequestPanel);
				for (InfoSystem temp : InfoStstems) 
				{
					submitRequestPanel.addElementToComboBox(temp.getName());
				}
				submitRequestPanel.setPhone(GlobalData.user.getPhone());
				
				submitRequestPanel.setApplicantName(GlobalData.user.getFirstName() + " " + GlobalData.user.getLastName());
				submitRequestPanel.setEmail(GlobalData.user.getEmail());
				if(GlobalData.worker!=null)
				   submitRequestPanel.setPosition(WorkerRolls.getWorkerRoleName(GlobalData.worker.getRole()));
				else  submitRequestPanel.setPosition(GlobalData.user.getDescriptionType());
			}
			return null;
		}
		@Override
		public void done() 
		{
		      
		}
	    };  
		worker.execute();
		
		
		
		
		
	}

	
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (findreqGUI!=null){
			if (e.getSource() == findreqGUI.getBtnSearch()){
				System.out.println("Search");
				SwingWorker worker = new SwingWorker<Void, Void>() {
					@Override
					 public Void doInBackground() 
					 {
						Request req = RequestManager.getInstance().getRequestByID(Integer.parseInt(findreqGUI.getTextFieldSerachID().getText()));
						if (req!=null){
							findreqGUI.getTextFieldDate().setText(req.getDate().toString());
							findreqGUI.getTextFieldInfoSystem().setText(req.getInfoSystem().getName());
							findreqGUI.getTextFieldInitiator().setText(req.getInitiator().getFirstName() +" "+ req.getInitiator().getLastName());
							findreqGUI.getTextPaneCurrentState().setText(req.getCurrentState());
							findreqGUI.getTextPaneDescription().setText(req.getDescription());
							findreqGUI.getTextPaneExscuse().setText(req.getExecuse());
							findreqGUI.getTxtpnNote().setText(req.getNotes());
							findreqGUI.getTextPaneProposedChange().setText(req.getProposedChange());
							
							findreqGUI.getTextFieldCurrentStatus().setText(req.getCurrentStatus().getStatus().getmStatus() +" - "+req.getCurrentStatus().getStatus().getsStatus());
							if (req.getFile()!=null)
							{
								findreqGUI.btnShowFile.setEnabled(true);
								findreqGUI.Filepath=req.getFile();
								System.out.println(findreqGUI.Filepath.toString());
							}
							
						}
						else{
							JOptionPane.showMessageDialog(new JFrame(),"I didnt found the reqyest you were looking for", "",JOptionPane.DEFAULT_OPTION);
						}
						return null;
					 }
					public void done(){
						
						
					}
				};
				worker.execute();
			}else if ( e.getSource() == findreqGUI.btnBack){
				GlobalData.guiMangaer.switchScreen(GlobalData.mainScreen);
			}
			
		}
		if(submitRequestPanel!=null)
		{
			if (e.getSource() == submitRequestPanel.btnBack)
			{	
				GlobalData.guiMangaer.switchScreen(GlobalData.mainScreen);
			}
			else if (e.getSource() == submitRequestPanel.btnSubnit)
			{	
				if(submitRequestPanel.isChosen() == true )
				{
					if(submitRequestPanel.getCurrentState().length() > 0)
					{
						if(submitRequestPanel.getChangeDescription().length() > 0)
						{
							if(submitRequestPanel.getExecuse().length() > 0)
							{
								String CurrentState = submitRequestPanel.getCurrentState();
								String ChangeDescription = submitRequestPanel.getChangeDescription();
								String Execuse = submitRequestPanel.getExecuse();
								InfoSystem infoSys = InfoStstems.get(submitRequestPanel.getChosen());
								
								String note = submitRequestPanel.getNote();
								PDFfile file = submitRequestPanel.getFile();
								
								Request req = new Request(CurrentState,ChangeDescription,Execuse,infoSys,note,file,GlobalData.user);
								int reqId = req.addNewRequestToDB();
								if(reqId!=-1)
								{
									JOptionPane.showMessageDialog(new JFrame(),"Request Added Successfully, The Request ID is" + reqId, "",JOptionPane.DEFAULT_OPTION);
									GlobalData.guiMangaer.switchScreen(GlobalData.mainScreen);
								}
								else
								{
									JOptionPane.showMessageDialog(new JFrame(),"There is a Problem whit the Conection, Please try again " + reqId, " ",JOptionPane.ERROR_MESSAGE);
								}
								
							}
							else
							{
								JOptionPane.showMessageDialog(new JFrame(),"To Continue you must Insert the Execuse to this Request ", "",JOptionPane.ERROR_MESSAGE);
							}
						}
						else
						{
							JOptionPane.showMessageDialog(new JFrame(),"To Continue you must Insert the Change Description ", "",JOptionPane.ERROR_MESSAGE);
						}
					}
					else
					{
						JOptionPane.showMessageDialog(new JFrame(),"To Continue you must Insert the Current State ", "",JOptionPane.ERROR_MESSAGE);
					}
				}
				else
				{
					JOptionPane.showMessageDialog(new JFrame(),"To Continue you must Choose Info System", "",JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		
	}

}
